{include file="common/header"/}

<body class="bg-light">
	<div class="container">
		<div class="d-flex">
			<div class="col-md-8">
				<div class="card mt-4">
					<div class="card-body">
						<div class="media">
							<a href="/index/person?user_id={$text.user.id}" class="text-dark a-notdecoration">
								<img src="__AVATOR__{$text.user.avator_path}" class="img-circle img-md">
							</a>
							<div class="media-body ml-2">
								<p class="my-1"><a href="/index/person?user_id={$text.user.id}" class="text-dark a-notdecoration"><strong>{$text.user.fullname}</strong></a></p>
								<p class="text-secondary"><small>{$text.create_time}</small></p>
							</div>
						</div>
						<hr class="my-0">
						<div class="mt-3">
							{$text.content}
						</div>
					</div>	
				</div>
			
				<div class="card my-2">
					<div class="card-body">
						<p class="card-title"><strong>最新回复({$replys|count})</strong></p>
						{foreach $replys as $reply}
						<div class="media">
							<img src="__AVATOR__{$reply.user.avator_path}" class="img-circle img-md">
							<div class="media-body ml-2">
								<p class="my-0"><strong>{$reply.user.fullname}</strong></p>
								<p class="my-0">{$reply.content}</p>
								<p class="text-secondary my-0"><small><i class="fa fa-clock-o"></i>&nbsp;{$reply.create_time|format_time}</small></p>
							</div>
						</div>
						<hr>
						{/foreach}
					</div>
				</div>
				
				{notempty name="Think.session.user_id"}
				<div class="card">
					<div class="card-body">
						<p class="card-title"><strong>添加回复</strong></p>
						<textarea id="reply_editor"></textarea>
						<button id="reply_submit" class="btn btn-primary mt-3">发表回复</button>
					</div>
				</div>
				{/notempty}
			</div>
		
			{notempty name="Think.session.user_id"}
			<div class="col-md-4">
				<div class="card text-center my-4">
					<img src="__AVATOR__{$Think.session.avator_path}" class="img-circle img-md mx-auto my-3">
					<p><strong>{$Think.session.fullname}</strong></p>
					<p>{$Think.session.introduction}</p>
				</div>
			</div>
			{/notempty}
		</div>
	</div>
</body>

<script type="text/javascript">
	$(function() {
		tinymce.init({
			selector: '#reply_editor',
		    language:'zh_CN',
		    height: 160,
		    menubar: false,
		    branding: false,
		    plugins: 'codesample, emoticons, wordcount, noneditable, atsomeone',
		    toolbar: [
		    	'emoticons codesample atsomeone bold italic underline blockquote strikethrough'
		    ],
		    codesample_languages: [
		        {text: 'Python', value: 'python'}
	   		],
		});

		$('#reply_submit').click(function() {
			var editor = tinymce.editors['reply_editor'];
			var content = editor.getContent();
			if (content == '') {
				toastr.warning('回复内容不能为空！');
				return ;
			}
			var wordcount_plugin = editor.plugins.wordcount;
			var character_count = wordcount_plugin.body.getCharacterCount();
			if (character_count > 150) {
				toastr.warning('字符数不能超过150！');
				return ;
			}

			// @ 检测
			var if_at = false;
			var at_list = [];
			var at_sign = 'at_user_id';
			var at_index = content.indexOf(at_sign);
			var content_ = content;
			while (at_index != -1) {
				if_at = true;
				let at_end = content_.indexOf('"', at_index);
				let at_id = content_.substring(at_index + at_sign.length + 1, at_end);
				at_list.push(at_id);
				content_ = content_.substring(at_index + at_sign.length);
				at_index = content_.indexOf(at_sign);
			}
			
			$.post("/index/text/addReply?text_id={$text.id}", {
				'content': content,
				'if_at': if_at,
				'at_list': at_list
			},
			function(result) {
				if (result.status == 'success') {
					toastr.success(result.message);
      				setTimeout(function(){
      					location.reload();
					}, 1000);
      			}
				if (result.status == 'error') {
      				toastr.error(result.message);
      			}
			}, "json");

		});
	});
</script>